package Control;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Donnees.Facade;
import Donnees.RepresentationBean;
import Donnees.SpectacleBean;

@SuppressWarnings("serial")
public class BilletterieServlet extends HttpServlet
{		
	public static int maxLignes = 30;
	
	public void doGet (HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		response.setCharacterEncoding("UTF-8");
		String redirection = "";
		if (request.getParameter("idSpectacle")!=null && request.getParameter("idSpectacle") != "" &&
				request.getParameter("idRepresentation")!=null && request.getParameter("idRepresentation") != "")
		{
			int idSpectacle = Integer.parseInt(request.getParameter("idSpectacle"));
			int indexSpectacle = Facade.getInstance().cmdRechercheIndexSpectacle(idSpectacle);	
			SpectacleBean spectacle = Facade.getInstance().cmdRechercheSpectacle(indexSpectacle);
			request.setAttribute("spectacle", spectacle);			
			int idRepresentation = Integer.parseInt(request.getParameter("idRepresentation"));
			int indexRepresentation = Facade.getInstance().cmdRechercheIndexRepresentation(spectacle, idRepresentation);
			RepresentationBean representation = Facade.getInstance().cmdRechercheRepresentation(spectacle, indexRepresentation);
			request.setAttribute("representation", representation);
			request.setAttribute("salle", representation.getSalle());
			request.setAttribute("nbSiegesDispos", representation.getSalle().getNbSieges());
			redirection = "/show.jsp";
		}
		else if(request.getParameter("idSpectacle")!=null && request.getParameter("idSpectacle") != "")
		{
			int noPage = 1;
			request.setAttribute("noPage", 1);
			
			if(request.getParameter("page") != null && Integer.parseInt(request.getParameter("page")) > 1)
			{
				noPage = Integer.parseInt(request.getParameter("page"));
				request.setAttribute("noPage", noPage);
			}
			
			int id = Integer.parseInt(request.getParameter("idSpectacle"));
			int index = Facade.getInstance().cmdRechercheIndexSpectacle(id);
			request.setAttribute("spectacle", Facade.getInstance().cmdRechercheSpectacle(index));			
			request.setAttribute("nbPages", Facade.getInstance().cmdNbPagesRepresentations(index, maxLignes));
			request.setAttribute("listeRepresentations", Facade.getInstance().cmdRechercheRepresentations(index, noPage, maxLignes));			
			redirection = "/showList.jsp";
		}
		else if(request.getParameter("humoriste") != null)
		{
			String humoriste = request.getParameter("humoriste");
			request.setAttribute("humoriste", humoriste);
			
			
			SpectacleBean[] listeRepresentationBean = Facade.getInstance().cmdRechercheRepresentations(humoriste);
			
			request.setAttribute("listeRepresentations", listeRepresentationBean);
			redirection = "/recherche.jsp";

		}
		else 
		{
			int noPage = 1;
			request.setAttribute("noPage", 1);
						
			if(request.getParameter("page") != null && Integer.parseInt(request.getParameter("page")) > 1)
			{
				noPage = Integer.parseInt(request.getParameter("page"));
				request.setAttribute("noPage", noPage);
			}
			
			request.setAttribute("nbPages", Facade.getInstance().cmdNbPagesSpectacles(maxLignes));
			request.setAttribute("listeSpectacles", Facade.getInstance().cmdRechercheSpectacles(noPage, maxLignes));		
			redirection = "/index.jsp";
		}	
		
		RequestDispatcher RequetsDispatcherObj = getServletContext().getRequestDispatcher(redirection);
		RequetsDispatcherObj.include(request, response);
	}
}
